package com.gyms.fragment;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.classic.okhttp.beans.HVAreaBean;
import com.gyms.R;
import com.gyms.adapter.AreaAdapter;
import com.gyms.base.BaseFragment;
import com.gyms.constant.Constants;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import click.OnRecyclerViewItemClick;
import constant.EventConstant;
import weight.UiEvent;

/**
 * Created by ltx on 17/12/28
 */

public class AreaFragment extends BaseFragment implements OnRecyclerViewItemClick {

    @BindView(R.id.rv_area)
    RecyclerView mRvArea;//地区列表
    public List<HVAreaBean> mHVAreaBeans;

    private AreaAdapter mAreaAdapter;

    private HVAreaBean mCurSelectedBean;//当前选中的bean

    private String mTag;//对应的标识

    @Override
    protected int getLayoutResId() {
        return R.layout.ll_area_fragment_layout;
    }

    @Override
    protected void initView() {
        initData();
        initAreaAdapter();
    }

    private void initAreaAdapter() {
        mAreaAdapter=new AreaAdapter(getActivity());
        mAreaAdapter.setHVAreaBeans(mHVAreaBeans);
        LinearLayoutManager mAreaManager=new LinearLayoutManager(getContext());
        mAreaManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRvArea.setLayoutManager(mAreaManager);
        mRvArea.setAdapter(mAreaAdapter);
    }

    @Override
    protected void initListener() {
        mAreaAdapter.setOnRecyclerViewItemClick(this);
    }

    @Override
    protected void processLogic() {

    }

    private void initData() {
        Bundle mBundle = getArguments();
        if (null != mBundle) {
            mHVAreaBeans = (List<HVAreaBean>) mBundle.getSerializable(Constants.S_KEY_AREA_LIST);
            mTag=mBundle.getString(Constants.S_KEY_AREA_TAG);
        }
    }

    /**
     * 实例化新的Fragment对象
     *
     * @param mAreaBeans mAreaBeans
     * @return AreaFragment AreaFragment
     */
    public static AreaFragment newIntance(ArrayList<HVAreaBean> mAreaBeans,String mTag) {
        AreaFragment mAreaFragment = new AreaFragment();
        Bundle mBundle = new Bundle();
        mBundle.putSerializable(Constants.S_KEY_AREA_LIST, mAreaBeans);
        mBundle.putString(Constants.S_KEY_AREA_TAG,mTag);
        mAreaFragment.setArguments(mBundle);
        return mAreaFragment;
    }

    @Override
    public void onRecyclerItemClick(RecyclerView view, int pos) {
        HVAreaBean mTempAreaBean=mHVAreaBeans.get(pos);
        mTempAreaBean.mTag=mTag;
        //更改选中状态
        doChangeSelectedBean(mTempAreaBean);
        mAreaAdapter.notifyDataSetChanged();
        //更换tab的状态 以及文案 并且如果所在的区不为null 就跳转下一个Fragment 否者直接返回
        EventBus.getDefault().post(new UiEvent(EventConstant.FLAG_SELECTED_AREA_BEAN,mTempAreaBean));
    }

    /**
     * 将原来的选中的bean的状态更换
     * @param mTempAreaBean 选中的bean
     */

    private void doChangeSelectedBean(HVAreaBean mTempAreaBean) {
        mTempAreaBean.isSelected=true;//当前选中的状态
        if(null!=mCurSelectedBean){//还原
            mCurSelectedBean.isSelected=false;
        }
        mCurSelectedBean=mTempAreaBean;
    }

    /**
     * 更新fragment数据源
     * @param mAreaBeans mAreaBeans
     */

    public void updateAreaBeans(List<HVAreaBean> mAreaBeans){
        if(mHVAreaBeans!=null){
            mHVAreaBeans.clear();
        }else{
            mHVAreaBeans=new ArrayList<>();
        }
        mHVAreaBeans.addAll(mAreaBeans);
        mAreaAdapter.notifyDataSetChanged();
    }

    @Override
    public void onDestroyView() {//将选择的bean 初始化
        if(null!=mCurSelectedBean){
            mCurSelectedBean.isSelected=false;
        }
        super.onDestroyView();
    }
}
